//
//  main.m
//  6-13复习解析电话薄
//
//  Created by 钟纪旺 on 16/6/13.
//  Copyright © 2016年 zjw. All rights reserved.
//

#import <Foundation/Foundation.h>




int main(int argc, const char * argv[]) {
    @autoreleasepool {
       NSString * path = @"/Users/zhongjiwang/Desktop/ccc.vcf";
        NSArray * data = @[@"大师"];
        NSError * err;
        NSString * vcf =[[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&err];
        if (err != 0) {
            NSLog(@"%@",err);
        }
        NSArray * arr =[vcf componentsSeparatedByString:@"\n"];
        NSMutableArray * abc = [NSMutableArray new];
  // NSMutableArray * dd =[NSMutableArray new];
        NSMutableDictionary *diti;
        for (NSString * line in arr) {
            if ([line hasPrefix:@"BEGIN"]) {
                NSLog(@"开始读取");
              diti = [NSMutableDictionary new];
            }if ([line hasPrefix:@"END"]) {
                NSLog(@"结束读取");
                [abc addObject:diti];
            }if ([line hasPrefix:@"FN"]) {
            NSArray * names =[line componentsSeparatedByString:@":"];
                NSString *name = names.lastObject;
                name = [name stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
                name = name.stringByRemovingPercentEncoding;
                
                diti[@"name"] =name;
                //把名字转化为拼音
                NSMutableString *mu =[NSMutableString stringWithString:name];
            
               CFStringTransform((CFMutableStringRef)mu, NULL, kCFStringTransformToLatin, false);
                NSLog(@"%@",mu);
                //把拼音首字母提出
                NSString *d =[mu substringWithRange:NSMakeRange(0, 1)];
                
                diti[@"dd"] = d;
               //  [dd addObject:d];
                
            
                
                 NSLog(@"%@",d);
            }if ([line hasPrefix:@"TEL"]) {
                NSLog(@"开始读取电话");
                
                NSArray * tels = [line componentsSeparatedByString:@":"];
                NSString * tel = tels.lastObject;
               
                diti[@"tel"] = tel;
                 NSLog(@"%@",tel);
                
            }
        }
       // NSLog(@"%@",dd);
        
        NSLog(@"%@",abc);
        
        [abc sortUsingComparator:^NSComparisonResult(NSDictionary *obj1, NSDictionary *obj2) {
            NSString *f1 = [obj1 objectForKey:@"dd"];
            NSString *f2 = [obj2 objectForKey:@"dd"];
            return [f1 compare:f2];
        }];
//        
//        NSMutableDictionary *dic = [NSMutableDictionary new];
//        NSMutableArray *ary = [NSMutableArray new];
//        [abc enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
////            NSLog(@"%@%@",[obj objectForKey:@"dd"],[obj objectForKey:@"name"]);
//            [ary addObject:[obj objectForKey:@"dd"]];
//            for (int i=0; i<ary.count;i++ ) {
//                for (int n=1; n<ary.count; n++) {
//                    if ([ary objectAtIndex:i]==[ary objectAtIndex:n]) {
//                        NSLog(@"%@",[ary objectAtIndex:n]);
//                    }
//                }
//                }
//                
//            }
//            ];
//        
//       // NSLog(@"%@",abc);
//        
        
        
        
        //创文件保存
        [data writeToFile:@"/Users/zhongjiwang/Desktop/sda.txt" atomically:NO];
         [abc writeToFile:@"/Users/zhongjiwang/Desktop/电话.txt" atomically:NO];
    }
    return 0;
}
